<template>
  <div class="user-manage">
    <!-- 上面的查询框 -->
    <div class="query-form">
      <el-form :inline="true" :model="queryForm" ref="form">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="queryForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-select v-model="queryForm.menuState">
            <el-option :value="1" label="正常"></el-option>
            <el-option :value="2" label="停用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="action">
        <el-button type="primary" @click="handleAdd(1)">新增</el-button>
      </div>
    </div>
    <!-- 下面的表格 -->
    <div class="base-table">
      <!-- 表单 -->
      <el-table :data="menuList" row-key="_id">
        <!-- 列头 -->
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          :formatter="item.formatter"
          :width="item.width"
        >
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="210">
          <template #default="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleAdd(2, scope.row)"
              >新增</el-button
            >
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 菜单新增 -->
    <el-dialog title="用户新增" v-model="showModal" @close="handleClose">
      <el-form
        ref="dialogForm"
        :model="menuForm"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="父级菜单" prop="parentId">
          <el-cascader
            v-model="menuForm.parentId"
            :options="menuList"
            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }"
            clearable
            placeholder="不选，则直接创建一级菜单"
          />
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item
          label="菜单图标"
          prop="icon"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.icon" placeholder="请输入菜单图标" />
        </el-form-item>
        <el-form-item
          label="路由地址"
          prop="path"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item
          label="组件名称"
          prop="component"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.component" placeholder="请输入组件名称" />
        </el-form-item>
        <el-form-item
          label="组件路径"
          prop="componentPath"
          v-show="menuForm.menuType == 1"
        >
          <el-input
            v-model="menuForm.componentPath"
            placeholder="请输入组件路径"
          />
        </el-form-item>
        <el-form-item
          label="菜单状态"
          prop="menuState"
          v-show="menuForm.menuType == 1"
        >
          <el-radio-group v-model="menuForm.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { formatDate } from '@/utils/date'

export default {
  name: 'Menu',
  data() {
    return {
      showModal: false,
      queryForm: {
        menuState: 1
      },
      menuList: [],
      columns: [
        {
          label: '菜单名称',
          prop: 'menuName',
          width: 180
        },
        {
          label: '图标',
          prop: 'icon'
        },
        {
          label: '权限标识',
          prop: 'menuCode'
        },
        {
          label: '路由地址',
          prop: 'path'
        },
        {
          label: '组件路径',
          prop: 'component'
        },
        {
          label: '菜单状态',
          prop: 'menuState',
          width: 80,
          formatter(row, column, value) {
            return {
              '1': '正常',
              '2': '停用'
            }[value]
          }
        },
        {
          label: '创建时间',
          prop: 'createTime',
          formatter(row, column, value) {
            return formatDate(value)
          }
        }
      ],
      menuForm: {
        parentId: [null],
        menuType: 1,
        menuState: 1
      },
      action: '',
      rules: {
        menuName: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2- 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 表单查询
    handleQuery() {
      this.getMenuList()
    },
    // 重置表单
    handleReset(form) {
      this.$refs[form].resetFields()
    },
    // 新增菜单
    handleAdd(type, row) {
      this.showModal = true
      this.action = 'add'
      if (type == 2) { // type 为 1 是最外层的新增，type 为 2 是表格尾部新增
        if (row.parentId[0] === null) {
          return this.menuForm.parentId = [row._id]
        }
        this.menuForm.parentId = [...row.parentId, row._id]
      }
    },
    // 编辑菜单
    handleEdit(row) {
      this.showModal = true
      this.action = 'edit'
      this.$nextTick(() => {
        Object.assign(this.menuForm, row)
      })
    },
    // 删除菜单
    async handleDel(_id) {
      this.$confirm('是否删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$api.menuSubmit({ _id, action: 'delete' })
        this.$message.success('删除成功')
        this.getMenuList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 取消新增弹窗
    handleClose() {
      this.showModal = false
      this.handleReset('dialogForm')
    },
    // 提交新增菜单信息
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          let { action, menuForm } = this
          let params = { ...menuForm, action }
          await this.$api.menuSubmit(params)
          this.showModal = false
          this.$message.success('操作成功')
          this.handleReset('dialogForm')
          this.getMenuList()
        }
      })
    },
    // 获取菜单列表
    async getMenuList() {
      try {
        let list = await this.$api.getMenuList(this.queryForm)
        this.menuList = list
        // console.log(list)
      } catch (e) {
        throw new Error(e)
      }
    }
  },
  mounted() {
    this.getMenuList()
  }
}
</script>

<style lang="scss" scoped>
.query-form {
  display: flex;
  justify-content: space-between;
}
</style>
